﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataAccess;

namespace QuanLyHopDong
{
    public partial class QuanLyHopDongListView : DevExpress.XtraEditors.XtraForm
    {
        private BindingList<BusinessObjects.QuanLyHopDong> bindingListQuanLyHopDong;
        private BusinessObjects.QuanLyHopDong _QuanLyHopDong;

        public QuanLyHopDongListView()
        {
            InitializeComponent();
            LoadData();
        }

        private void QuanLyHopDongListView_Load(object sender, EventArgs e)
        {
            string[] listFieldName = new string[] {"Oid", "NamHoc.TenNamHoc" };
            string[] listColumnName = new string[] { "Mã", "Năm học" };
            AppGridView.InitGridView(gridViewQuanLyHopDong, false, false, DevExpress.XtraGrid.Views.Grid.GridMultiSelectMode.RowSelect, false, false, false, true);
            AppGridView.ReadOnlyGridView(gridViewQuanLyHopDong);
            AppGridView.ShowField(gridViewQuanLyHopDong, listFieldName, listColumnName);
            LoadData();
        }

        private void LoadData()
        {
            bindingListQuanLyHopDong = new BindingList<BusinessObjects.QuanLyHopDong>(DataAccessObject.QuanLyHopDong.GetListQuanLyHopDong());

            bsQuanLyHopDong.DataSource = bindingListQuanLyHopDong;
            gridViewQuanLyHopDong.Invalidate();
        }

        private void btnThem_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            _QuanLyHopDong = new BusinessObjects.QuanLyHopDong();
            OpenDetailForm();
        }

        private void btnXoa_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            DeleteRow();
        }

        private void btnLamMoi_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            LoadData();
        }

        private void btnDong_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Close();
        }

        private void gridViewQuanLyHopDong_DoubleClick(object sender, EventArgs e)
        {
            _QuanLyHopDong = gridViewQuanLyHopDong.GetFocusedRow() as BusinessObjects.QuanLyHopDong;
            OpenDetailForm();
        }

        private void OpenDetailForm()
        {
            QuanLyHopDongDetailView detailView = new QuanLyHopDongDetailView(_QuanLyHopDong);
            detailView.SaveChanged += new EventHandler(detailView_SaveChanged);
            detailView.MdiParent = this.MdiParent;
            detailView.Show();
        }

        void detailView_SaveChanged(object sender, EventArgs e)
        {
            gridViewQuanLyHopDong.Invalidate();
        }

        private void DeleteRow()
        {
            if (MessageBox.Show("Bạn có chắc xóa?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.Yes)
            {
                _QuanLyHopDong = gridViewQuanLyHopDong.GetFocusedRow() as BusinessObjects.QuanLyHopDong;
                if (_QuanLyHopDong != null && _QuanLyHopDong.Oid > 0)
                {
                    if (DataAccessObject.QuanLyHopDong.Delete(_QuanLyHopDong) > 0)
                    {
                        gridViewQuanLyHopDong.DeleteSelectedRows();
                        gridViewQuanLyHopDong.Invalidate();
                    }
                    else
                        MessageBox.Show("Không xóa được", "Có Lỗi Xảy Ra", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
        }
    }
}